#include "mainscene.h"
#include "config.h"
#include <QIcon>
#include <QPainter>
#include <QMouseEvent>
#include <ctime>
#include <QSound>
MainScene::MainScene(QWidget *parent)
    :QWidget(parent)
{
    //调用初始化场景
    initScene();
    //启动游戏
    playGame();


}

MainScene::~MainScene()
{

}

void MainScene::initScene()
{
    //设置窗口固定尺寸
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //设置标题
    setWindowTitle(GAME_TITLE);
    //加载图标
    setWindowIcon(QIcon(GAME_ICON));
    //定时器设置
    m_Timer.setInterval(GAME_RATE);
    //敌机出场间隔 初始化
    m_recorder=0;
    //随机数种子
    srand((unsigned int)time(NULL));

      //xiugai

    m_hero2.setPosition(GAME_WIDTH * 0.25 - m_hero2.m_Plane.width()*0.5,
                       GAME_HEIGHT - m_hero2.m_Plane.height());
      // 初始化键盘状态
    for(int i=0; i<4; i++) {
        m_keyPressed[i] = false;
        m_keyPressed2[i] = false;
    }


}

void MainScene::playGame()
{
    //启动背景音乐
    QSound::play(SOUND_BACKGROUND);


 //启动定时器
    m_Timer.start();
 //监听定时器发送的信号
    connect(&m_Timer,&QTimer::timeout,[=](){
        //敌机出场
        enemyToScene();
        //更新游戏中元素的坐标
        updatePosition();
        //绘制到屏幕中
        update();
        //碰撞检测
        collisionDetection();
    });

}

void MainScene::updatePosition()
{
    //更新地图的坐标
    m_map.mapPosition();

    //测试子弹
    //temp_Bullet.m_Free =false;
    //temp_Bullet.updatePostion();

    //发射子弹
    m_hero.shoot();

    //计算所有非空闲子弹的当前坐标
    for(int i=0;i<BULLET_NUM;i++)
    {
        //如果非空闲，计算发射位置
        if(m_hero.m_bullets[i].m_Free==false)
        {
            m_hero.m_bullets[i].updatePostion();
        }
    }
    //敌机出场
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemy[i].m_Free==false)
        {
            m_enemy[i].updatePosition();
        }
    }
    //计算Bomb的播放的图片
    for(int i=0;i<BOMB_NUM;i++)
    {
       if(m_bombs[i].m_Free==false)
       {
           m_bombs[i].updateInfo();
       }
    }


}

void MainScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);

    //绘制飞机
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);

    //测试子弹
    //painter.drawPixmap(temp_Bullet.m_X,temp_Bullet.m_Y,temp_Bullet.m_Bullet);

    //绘制子弹
    for(int i=0;i<BULLET_NUM;i++)
    {
        //如果非空闲，绘制
        if(m_hero.m_bullets[i].m_Free==false)
        {
        painter.drawPixmap(m_hero.m_bullets[i].m_X,m_hero.m_bullets[i].m_Y,m_hero.m_bullets[i].m_Bullet);
       }
    }
    //绘制敌机
    for(int i=0;i<ENEMY_NUM;i++){
        if(m_enemy[i].m_Free==false)
        {
            painter.drawPixmap(m_enemy[i].m_X,m_enemy[i].m_Y,m_enemy[i].m_enemy);
        }
    }
   //绘制爆炸
    for(int i=0;i<BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free==false)
        {
            painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);
        }
    }

}
//双人版舍弃鼠标
/*
void MainScene::mouseMoveEvent(QMouseEvent *event)
{
    //setMouseTracking(true); //点一下即可黏住
  int x=  event->x()-m_hero.m_Rect.width()*0.5;
  int y=  event->y()-m_hero.m_Rect.height()*0.5;
  //边界检测
  if(x<=0)
  {
      x=0;
  }
  if(x>=GAME_WIDTH-m_hero.m_Rect.width())
  {
  x=GAME_WIDTH-m_hero.m_Rect.width();
  }
  if(y<=0)
  {
      y=0;
  }
  if(y>=GAME_HEIGHT-m_hero.m_Rect.height())
  {
      y=GAME_HEIGHT-m_hero.m_Rect.height();
  }
  m_hero.setPosition(x,y);
}
*/
void MainScene::collisionDetection()
{
    //遍历所有非空闲的敌机
    for(int i=0;i<ENEMY_NUM;i++)
    {
        //如果是空闲的飞机，执行下一次循环
        if(m_enemy[i].m_Free)
        {
            continue;
        }
        //遍历所有非空闲的子弹
        for(int j=0;j<BULLET_NUM;j++)
        {
            //空闲子弹，执行下一次循环
          if(m_hero.m_bullets[i].m_Free)
          {
              continue;
          }
          //如果子弹和飞机相交，发生碰撞，同时将飞机和子弹空闲状态设置为真
          if(m_enemy[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect))
          {
              m_enemy[i].m_Free=true;
              m_hero.m_bullets[j].m_Free=true;

              //播放爆炸效果
              for(int k=0;k<BOMB_NUM;k++)
              {   //空闲的爆炸，可以播放
                  if(m_bombs[k].m_Free)
                  {
                      //播放爆炸音效
                      QSound::play(SOUND_BOMB);

                  m_bombs[k].m_Free=false;

                  m_bombs[k].m_X=m_enemy[i].m_X;
                  m_bombs[k].m_Y=m_enemy[i].m_Y;
                  break;
                  }

              }

           }
         }
    }
}
void MainScene::enemyToScene()
{
    m_recorder++;
    //未达到出场时间
    if(m_recorder<ENEMY_INTERVAL)
    {
        return;
    }
    m_recorder=0;
    for(int i=0;i<ENEMY_NUM;i++)
    {
        //如果空闲，敌机出场
        if(m_enemy[i].m_Free)
        {
            m_enemy[i].m_Free=false;

            //敌机坐标
            m_enemy[i].m_X=rand()%(GAME_WIDTH-m_enemy[i].m_Rect.width());
            m_enemy[i].m_Y=-m_enemy[i].m_Rect.height();
            break;
        }
    }

}

//xiugai
void MainScene::keyPressEvent(QKeyEvent *e)
{
    switch(e->key()) {
    // 玩家1 (WSAD)
    case Qt::Key_W: m_keyPressed[0] = true; break;
    case Qt::Key_S: m_keyPressed[1] = true; break;
    case Qt::Key_A: m_keyPressed[2] = true; break;
    case Qt::Key_D: m_keyPressed[3] = true; break;

    // 玩家2 (小键盘方向键)
    case Qt::Key_Up:    m_keyPressed2[0] = true; break;
    case Qt::Key_Down:  m_keyPressed2[1] = true; break;
    case Qt::Key_Left:  m_keyPressed2[2] = true; break;
    case Qt::Key_Right: m_keyPressed2[3] = true; break;
    }
}

void MainScene::keyReleaseEvent(QKeyEvent *e)
{
    switch(e->key()) {
    // 玩家1 (WSAD)
    case Qt::Key_W: m_keyPressed[0] = false; break;
    case Qt::Key_S: m_keyPressed[1] = false; break;
    case Qt::Key_A: m_keyPressed[2] = false; break;
    case Qt::Key_D: m_keyPressed[3] = false; break;

    // 玩家2 (小键盘方向键)
    case Qt::Key_Up:    m_keyPressed2[0] = false; break;
    case Qt::Key_Down:  m_keyPressed2[1] = false; break;
    case Qt::Key_Left:  m_keyPressed2[2] = false; break;
    case Qt::Key_Right: m_keyPressed2[3] = false; break;
    }
}

